package com.example.demo.leetcode;

import java.util.PriorityQueue;

/**
 * 链接：<a href="https://leetcode.cn/problems/p0NxJO/description/?envType=daily-question&envId=2024-02-06">https://leetcode.cn/problems/p0NxJO/description/?envType=daily-question&envId=2024-02-06</a>
 *
 * @author WangYX
 * @version 1.0.0
 * @date 2024/02/06 11:40
 */
public class LCP_30_魔塔游戏 {

    public static void main(String[] args) {
        int[] nums = {100, 100, 100, -250, -60, -140, -50, -50, 100, 150};
        int i = magicTower(nums);
        System.out.println(i);
    }

    public static int magicTower(int[] nums) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
        int ans = 0;
        long hp = 1, delay = 0;
        for (int num : nums) {
            if (num < 0) {
                pq.offer(num);
            }
            hp += num;
            if (hp <= 0) {
                ++ans;
                int curr = pq.poll();
                delay += curr;
                hp -= curr;
            }
        }
        hp += delay;
        return hp <= 0 ? -1 : ans;
    }

}
